package com.ruoyi.web.controller.zkcontroller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.ruoyi.web.domain.Employee;
import com.ruoyi.web.service.zkService.EmployeeService;
import com.ruoyi.web.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 店家管理-判断用户名密码是否能成功登陆
     *
     * @param request
     * @param employee 封装前段发送来的用户名和密码
     * @return 返回封装好的R数据
     */
    //@RequestBody 主要用于接收前端传递给后端的json字符串（请求体中的数据）
    //HttpServletRequest request作用：如果登录成功，将员工对应的id存到session一份，这样想获取一份登录用户的信息就可以随时获取出来
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request,@RequestBody Employee employee) {

        // 1.将页面提交的密码进行MD5加密
        String password = employee.getPassWord();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2.根据页面提交的用户名查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUserName,employee.getUserName());
        Employee emp = employeeService.getOne(queryWrapper);
        System.out.println(emp);

        if(emp == null){
            return R.error("登陆失败");
        }

        if(!emp.getPassWord().equals(password)) {
            return R.error("登陆失败");
        }
        request.getSession().setAttribute("employee",emp.getId());


        return R.ok(emp);

    }

    @PostMapping("/logout")
    @ResponseBody
    public R<String> login(HttpServletRequest request) {
        //1.清除Session
        HttpSession session = request.getSession();
        session.removeAttribute("employee");
        //2.返回结果
        return R.ok("退出");
    }

    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("", employee.toString());
        employee.setPassWord(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeService.save(employee);
        return R.ok("新增员工成功");
    }

    /**
     * 店家信息分页查询
     *
     * @param page     当前页面
     * @param pageSize 每页的条数
     * @param name     查询名字
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        log.info("", page, pageSize, name);
        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);
        /*
        构造条件构造器
        通过like进行过滤查询
        通过orderBy进行排序
         */
        LambdaQueryWrapper<Employee> Wrapper = new LambdaQueryWrapper<>();
        Wrapper.like(StringUtils.isEmpty(name), Employee::getName, name);
        Wrapper.orderByDesc(Employee::getUpdateTime);

        return R.ok(pageInfo);
    }

    /**
     * 商家管理——更新职位表数据
     *
     * @param request
     * @param employee 传入商家管理员信息
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("", employee.toString());

        //获取修改人的id
        Long update = (Long) request.getSession().getAttribute("employee");

        //进行更新
        employeeService.updateById(employee);

        //返回结果
        return R.ok("成功更新");
    }

    /**
     * 根据id进行查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable("id") long id){
        log.info("接收到根据id查询请求 id：{}",id );
        Employee byId = employeeService.getById(id);
        if(byId!=null){
            return R.ok(byId);
        }
        return R.error("没查到用户信息");
    }
}
